package com.sinodata.bsm.center.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.ResViewDao;
import com.sinodata.bsm.common.vo.ResView;

/**
 * <p>
 * Description: 资源视图DAO实现类
 * </p>
 * 
 * @author dongyushi
 * @version 1.0
 * 
 * <p>
 * History:
 * 
 * Date Author Version Description
 * ---------------------------------------------------------------------------------
 * 2012-5-21 PM 1:29:45 dongyushi 1.0 To create
 * </p>
 * 
 * @since
 * @see
 */
@Repository
public class ResViewDaoImpl extends BaseDao implements ResViewDao {

    /*
     * (non-Javadoc)
     * 
     * @see com.sinodata.bsm.center.dao.ResViewDao#findByPage(com.sinodata.bsm.center.bean.Page)
     */
    @Override
    public Page<ResView> findByPage(Page<ResView> page) {
        page.setTotal(getTotal());
        StringBuilder hql = new StringBuilder(" from ResView w where 1=1 and w.id>9 order by id  ");
        @SuppressWarnings("unchecked")
        List<ResView> list = getSession().createQuery(hql.toString()).setFirstResult(page.getFirstResultNo()).setMaxResults(page.getPageSize()).list();
        page.setRows(list);
        return page;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sinodata.bsm.center.dao.ResViewDao#batchDelete(java.lang.Long[])
     */
    @Override
    public void batchDelete(Long[] ids) {
        this.getSession().createQuery("delete from ResView where id in (:ids)").setParameterList("ids", ids).executeUpdate();
    }

    /**
     * 获取总数
     * 
     * @return
     */
    private int getTotal() {
        StringBuilder hql = new StringBuilder(" select count(*) from ResView where 1=1 ");
        Query query = this.getSession().createQuery(hql.toString());
        return ((Long) query.list().iterator().next()).intValue();
    }

    public List<Long> findChildByViewId(Long viewId, List<Long> privilegeRess) {
        List<Long> results = new ArrayList<Long>();
        StringBuffer bf = new StringBuffer();
        bf.append(" select * from (");
        bf.append(" select end_id id from res_relation where relation_type_id<200 start with start_id in (");
        bf.append(" select res_id from res_view_rel where res_view_id= ").append(viewId).append(" )");
        bf.append(" connect by prior end_id=start_id ");
        bf.append(" union select res_id from res_view_rel where res_view_id= ").append(viewId);
        bf.append(") A where A.id in(");
        boolean flag = true;
        for (Long resId : privilegeRess) {
            if (flag) {
                bf.append(resId);
                flag = false;
            } else {
                bf.append("," + resId);
            }
        }
        bf.append(")");

        List<BigDecimal> list = getSession().createSQLQuery(bf.toString()).list();

        if (list != null)
            for (BigDecimal bd : list) {
                results.add(bd.longValue());
            }
        return results;
    }

    public Long findMotiResType(Long resTypeId) {
        String sql = "select moit_type_id from moti_restype_rel t where res_type_id in (";
        sql += "select id from res_type";
        sql += " start with id=" + resTypeId;
        sql += " connect by prior parent_id = id )";

        List<BigDecimal> list = getSession().createSQLQuery(sql).list();
        if (list != null)
            return list.get(0).longValue();
        return null;
    }

}